import { findAllCategory } from "@/api/category"
import { topCategory } from "@/api/constants"

// 分类模块
export default {
    namespaced: true,
    state () {
        return {
            // 分类信息集合
            list: topCategory.map(item=>({name:item}))
        }
    },
    mutations: {
        setCategory(state,headCategory){
            state.list = headCategory
        },
        // 修改当前一级分类下的open数据为true
        show (state, item) {
            const category = state.list.find(category => category.id === item.id)
            category.open = true
        },
        // 修改当前一级分类下的open数据为false
        hide (state, item) {
            const category = state.list.find(category => category.id === item.id)
            category.open = false
        }
    },
    actions: {
        async getList({commit}){
            const { result } = await findAllCategory()
            result.forEach(item => {
                item.open = false
            })
            // 获取数据成功，提交mutations进行数据修改
            commit('setCategory', result)
        }
    }
}